package com.yex.service.modify;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yex.common.exception.RRException;
import com.yex.entity.UserEntity;
import com.yex.service.ApiService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

public class BaseService<Q extends BaseMapper<T>, T> extends ServiceImpl<Q, T> {

    @Autowired
    private ApiService apiService;

    protected UserEntity validateLoginUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (StringUtils.isBlank(token)) {
            token = request.getParameter("token");
        }
        if (StringUtils.isBlank(token)) throw new RRException("not_login");
        UserEntity userEntity = apiService.verifyToken(token);
        if (userEntity == null) throw new RRException("not_login");
        return userEntity;
    }

}
